package com.fenqing.leetcode.code.t1300_1400;

import com.fenqing.annotations.Execute;
import com.fenqing.bean.TreeNode;
import com.fenqing.core.Topic;

public class Topic1302 extends Topic {

    private int sum = 0;

    @Execute
    public int deepestLeavesSum(TreeNode root) {
        int max = maxDeep(root, 0);
        deepestLeavesSum(root, max, 0);
        return sum;
    }

    public int maxDeep(TreeNode root, int deep){
        if (root == null) {
            return deep - 1;
        }
        return Math.max(maxDeep(root.left, deep + 1), maxDeep(root.right, deep + 1));
    }

    public void deepestLeavesSum(TreeNode root, int max, int now) {
        if (root == null) {
            return;
        }
        if (max == now) {
            sum += root.val;
        } else {
            deepestLeavesSum(root.left, max, now + 1);
            deepestLeavesSum(root.right, max, now + 1);
        }

    }

    public static void main(String[] args) {
        Topic1302 topic1302 = new Topic1302();
        Object run = topic1302.run("[1,2,3,4,5,null,6,7,null,null,null,null,8]");
        System.out.println(run);
    }

}
